package nii.game.mobileserio.enemy;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.entity.layer.tiled.tmx.TMXLayer;
import org.anddev.andengine.entity.layer.tiled.tmx.TMXProperties;
import org.anddev.andengine.entity.layer.tiled.tmx.TMXTile;
import org.anddev.andengine.entity.layer.tiled.tmx.TMXTileProperty;
import org.anddev.andengine.entity.layer.tiled.tmx.TMXTiledMap;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;

import android.content.Context;

public class Goomba {
	
	//DECLARATION ==========================================
	int TYPE = 0;

	private BitmapTextureAtlas goomba_BitmapTextureAtlas;
	private TiledTextureRegion goomba_TiledTextureRegion;
	public AnimatedSprite goomba_AnimatedSprite;

	private float positionX = 0;
	private float positionY = 0;
	private boolean is_MoveLeft = true;
	
	//Maps ============================
	private TMXTiledMap mTMXTiledMap;
	private TMXLayer VatCanTMXLayer;
	
	//Khai bao Scene ==================
	private Scene mScene;

	public Goomba() {
	};

	//========================================|| onLoadResources ||===========================================//
	
	public void onLoadResources(Engine mEngine, Context mContext) {
		
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
		this.goomba_BitmapTextureAtlas = new BitmapTextureAtlas(256, 64, TextureOptions.DEFAULT);
		this.goomba_TiledTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.goomba_BitmapTextureAtlas, mContext,
						"turtle.png", 0, 0, 4, 1);
		mEngine.getTextureManager().loadTexture(this.goomba_BitmapTextureAtlas);

	}

	//==========================================|| onLoadScene ||=============================================//
	
	public void onLoadScene(Scene mScene) 
	{

		this.mScene = mScene;
		
		goomba_AnimatedSprite = new AnimatedSprite(this.positionX, this.positionY, this.goomba_TiledTextureRegion.deepCopy());
		goomba_AnimatedSprite.setCullingEnabled(true);
		showGoombaStatus();
		mScene.attachChild(goomba_AnimatedSprite);
	}

	// moveX =====================	
	public void moveX(float x) {
		this.positionX = x;
		moveGoomba();
	}

	// moveY =====================
	public void moveY(float y) {
		this.positionY = y;
		moveGoomba();
	}

	// moveGoomba ========================================================
	private void moveGoomba() {
		goomba_AnimatedSprite.setPosition(this.positionX, this.positionY);
	}

	// setStatusGoomba ===================================================
	public void setStatusGoomba(int status) {
		this.TYPE = status;
		showGoombaStatus();
	}

	// getStatusGoomba ==========================================
	public int getStatusGoomba() {
		return this.TYPE;
	}

	// setPositionXY ============================================
	public void setPositionXY(float positionX, float positionY) {
		this.positionX = positionX;
		this.positionY = positionY;
	}

	// showGoombaStatus =========================================
	private void showGoombaStatus() {

		switch (TYPE) {
		
		case 0:// left
		{
			goomba_AnimatedSprite.animate(new long[] { 100, 100 }, 2, 3 , true);
			break;
		}
		case 1:// right
		{
			goomba_AnimatedSprite.animate(new long[] { 100, 100 }, 0, 1 , true);
			break;
		}

		}

	}

	// moveLeft =======================================================================
	public void moveLeft(float pX) {
		if (collidesWith(goomba_AnimatedSprite.getX() - 3, goomba_AnimatedSprite.getY() + goomba_AnimatedSprite.getHeight())){
			is_MoveLeft = false;
		}
		else{
			setStatusGoomba(0);
			goomba_AnimatedSprite.setPosition(goomba_AnimatedSprite.getX() - pX,
					goomba_AnimatedSprite.getY());
		}
		
	}

	// moveRight =======================================================================
	public void moveRight(float pX) {
		if (collidesWith(goomba_AnimatedSprite.getX() + goomba_AnimatedSprite.getWidth() + 3, goomba_AnimatedSprite.getY() + goomba_AnimatedSprite.getHeight())){
			is_MoveLeft = true;
		}
		else {
			setStatusGoomba(1);
			goomba_AnimatedSprite.setPosition(goomba_AnimatedSprite.getX() + pX,
					goomba_AnimatedSprite.getY());
		}
	}

	// moveRandom =======================================================================
	public void moveRandom(float pX) {
		if (is_MoveLeft == false)
			moveRight(pX);
		else 
			moveLeft(pX);
	}
	
	//Thiet dat map ======================================
	public void setTMXTiledMap(TMXTiledMap mTMXTiledMap){
		this.mTMXTiledMap = mTMXTiledMap;
	}
	
	//Thiet dat vat can ==================================
	public void setTMXLayer(TMXLayer VatCanTMXLayer){
		this.VatCanTMXLayer = VatCanTMXLayer;
	}
	
	//Kiem tra va cham ===================================
	public boolean collidesWith(float pX, float pY){
		TMXTile mTMXTile = VatCanTMXLayer.getTMXTileAt(pX,pY);
    	try{
			if(mTMXTile == null){
				System.out.println("mTMXTile = null");
			}
			else{
				TMXProperties<TMXTileProperty> mTMXProperties= mTMXTile.getTMXTileProperties(mTMXTiledMap);
				TMXTileProperty mTMXTileProperty = mTMXProperties.get(0);
				if(mTMXTileProperty.getName().equals("vatcan")){
				}
			}
			
			return true;
		}catch(Exception e){
			return false;
		}
	}
	
	// deletegoomba =======================================
	public void deletegoomba(){
		mScene.detachChild(goomba_AnimatedSprite);
	}
}
